#Copyright ReportLab Europe Ltd. 2000-2004
#see license.txt for license details
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/demos/odyssey/odyssey.py
__version__=''' $Id: odyssey.py 2385 2004-06-17 15:26:05Z rgbecker $ '''
___doc__=''
#odyssey.py
#
#Demo/benchmark of PDFgen rendering Homer's Odyssey.



from reportlab.pdfgen import canvas
import os, sys

#find out what platform we are on and whether accelerator is
#present, in order to print this as part of benchmark info.
try:
    import _rl_accel
    ACCEL = 1
except ImportError:
    ACCEL = 0




from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import letter

#precalculate some basics
top_margin = letter[1] - inch
bottom_margin = inch
left_margin = inch
right_margin = letter[0] - inch
frame_width = right_margin - left_margin


def drawPageFrame(canv, title):
    canv.line(left_margin, top_margin, right_margin, top_margin)
    canv.setFont('Times-Italic',12)
    canv.drawString(left_margin, top_margin + 2, title)
    canv.line(left_margin, top_margin, right_margin, top_margin)


    canv.line(left_margin, bottom_margin, right_margin, bottom_margin)
    canv.drawCentredString(0.5*letter[0], 0.5 * inch,
               "Page %d" % canv.getPageNumber())



def run(verbose=1):
    if sys.platform[0:4] == 'java':
        impl = 'Jython'
    else:
        impl = 'Python'
    verStr = '%d.%d' % (sys.version_info[0:2])
    if ACCEL:
        accelStr = 'with _rl_accel'
    else:
        accelStr = 'without _rl_accel'

    title = "Web Developer Access Report"
    canv = canvas.Canvas('user.pdf', invariant=1)
    canv.setPageCompression(1)
    drawPageFrame(canv, title)

    #do some title page stuff
    canv.setFont("Times-Bold", 36)
    canv.drawCentredString(0.5 * letter[0], 7 * inch, title)

    canv.setFont("Times-Bold", 18)
    canv.drawCentredString(0.5 * letter[0], 5 * inch, "EriKin Web Hosting")

    canv.showPage()
    #on with the text...
    drawPageFrame(canv, title)

    canv.setFont('Times-Roman', 12)
    tx = canv.beginText(left_margin, top_margin - 0.5*inch)

    data = [["www.unt.edu:", ["test user", "tuser"]]]
    fullline = 90
    header = "Virtual Host" + " "*17 + "| User Full Name" + " "*13 + "| User ID "
    tx.textLine(header)
    canv.line(left_margin, top_margin - 0.55*inch, right_margin-2*inch, top_margin - 0.55*inch)
    
    for line in data[0]:
        #userList = line[1]
        newLine = line
        print "newLine == ", newLine
        if type(newLine) == list:
            spacer = " "*37
            text = spacer
            for i in newLine:
                text = text + "| " + i + " "*(30 - (len(i)/2))
            tx.textLine(text)
        else:
            tx.textLine(newLine)
        canv.drawText(tx)

        #page breaking
        y = tx.getY()   #get y coordinate
        if y < bottom_margin + 0.5*inch:
            canv.drawText(tx)
            canv.showPage()
            drawPageFrame(canv, title)
            canv.setFont('Times-Roman', 12)
            tx = canv.beginText(left_margin, top_margin - 0.5*inch)

            #page
            pg = canv.getPageNumber()
            if verbose and pg % 10 == 0:
                print 'formatted page %d' % canv.getPageNumber()

    if tx:
        canv.drawText(tx)
        canv.showPage()
        drawPageFrame(canv, title)

    if verbose:
        print 'about to write to disk...'

    canv.save()


if __name__=='__main__':
    quiet = ('-q' in sys.argv)
    run(verbose = not quiet)
